package com.sxx.beautifulcity.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import com.baidu.platform.comapi.map.J;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.service.BackGroundMusic;
import com.sxx.beautifulcity.view.Titanic;
import com.sxx.beautifulcity.view.TitanicTextView;

import cn.jpush.android.api.JPushInterface;

import static com.sxx.beautifulcity.R.raw.beautiful;

/**
 * Created by dllo on 15/11/2.
 * 闪屏界面
 */
public class SplanshActivity extends AppCompatActivity {
    private Handler handler;//定义线程
    private SharedPreferences sp;//定义缓存文件
    private Boolean isAutoLogin;//定义是否自动登录状态
    private Titanic titanic;//定义自定义的波浪类型的类
    private TitanicTextView titanic_tv;//定义自定义的TitanicTextView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_splash);
        //加载布局
        final View view = View.inflate(this, R.layout.activity_splash, null);
        //设置布局
        setContentView(view);
        //初始化
        titanic_tv = (TitanicTextView) findViewById(R.id.titanic_tv);
        titanic = new Titanic();
        titanic.start(titanic_tv);
        titanic.cancel();

        //自动登录标识
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        isAutoLogin = sp.getBoolean("isAutoLogin", false);
        startService(new Intent(SplanshActivity.this , BackGroundMusic.class));
        //渐变展示启动屏
        AlphaAnimation aa = new AlphaAnimation(0.1f, 1.0f);
        aa.setDuration(2000);
        view.startAnimation(aa);
        aa.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationEnd(Animation arg0) {
                redirectTo();
                Log.i("log", "触发了事件");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationStart(Animation animation) {
            }

        });
    }

    /**
     * 跳转到主程序的代码
     */
    private void redirectTo() {

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {

                Intent intent = new Intent();
                // 当设置了自动登录并且用户名和密码均不为空 则直接跳转到主页面
                if (isAutoLogin) {

                    intent.setClass(SplanshActivity.this, MainActivity.class);
                } else {
                    // 否则跳转到注册界面 用户需要先登录
                    intent.setClass(SplanshActivity.this, RegisterActivity.class);
                }
                startActivity(intent);
                finish();
                stopService(new Intent(SplanshActivity.this, BackGroundMusic.class));
                overridePendingTransition(R.anim.left_out, R.anim.right_into);
                return false;
            }
        });
        handler.sendEmptyMessageDelayed(100, 3000);
    }
    //极光推送
    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }
    //极光推送
    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }
}

